<?php
/**
 * Description:
 *
 * @author      Eden
 * @datetime    2017/9/11 07:51
 * @copyright   WuHan HuQi Technology Co.,Ltd.
 * @url         http://cnhuqi.com
 */

class Purchaser extends ADM_Controller
{
    const PAGE_SIZE = 10;

    /**
     * 构造函数
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $lists = [];
        $pageStr = '';
        $status = 0;
        do {
            $params = $this->input->get();
            $page = !empty($params['page']) && intval($params['page']) > 0 ? $params['page'] : 1;
            $offset = ($page - 1) * self::PAGE_SIZE;
            $limit = self::PAGE_SIZE;

            $status = value($params, 'status');
            if (empty($params)) {
                $status = -1;
            }

            $this->load->model('common/Caigou_Model', 'm');
            $total = $this->m->getTotal($status);
            if ($total == 0) {
                break;
            }

            $lists = $this->m->getUsers($status, $offset, $limit);

            if (!empty($lists)) foreach($lists as &$item) {
                $item['license'] = thumb($item['license_url']);
                $item['license_url'] = thumb($item['license_url'], 30, 50);
                $item['organization'] = thumb($item['organization_url']);
                $item['organization_url'] = thumb($item['organization_url'], 30, 50);
            }

            $this->load->library('core/Page', null, 'p');
            $url = base_url('/user/purchaser/index');
            $pageStr = $this->p->getPage($url, $total, self::PAGE_SIZE);

        } while(false);

        $this->load->view('admin/user/purchaser/index.html', array(
            'status' => $status,
            'lists' => $lists,
            'page' => $pageStr
        ));
    }

    /**
     * 审核
     */
    public function doAudit()
    {
        $send['state'] = false;
        do {
            $params = $this->input->post();
            if (empty($params['id']) || empty($params['action'])) {
                $send['error'] = '参数错误';
                break;
            }

            $this->load->model('common/Caigou_Model', 'm');
            $item = $this->m->getPurchaserItem($params['id']);
            if (empty($item)) {
                $send['error'] = '该申请不存在';
                break;
            }

            if (($item['status'] == 5 && $params['action'] == 'reject') || ($item['status'] == 6 && $params['action'] == 'pass')) {
                $send['error'] = '该申请状态不符';
                break;
            }

            $status = $params['action'] == 'pass' ? 6 : 5;
            $res = $this->m->modifyStatus($params['id'], $status);
            if ($res) {

                $this->load->model('admin/user/User_Model', 'u');
                $this->u->setUserType($item['userid'], $status == 6 ? 1 : 0);

                if ($params['action'] == 'pass') {
                    $tpl = '恭喜您,您已所提交的丘丘谷商城采购商申请已通过审核,请登录查看【丘丘谷】';
                } else {
                    $tpl = '很遗憾通知您,您提交的丘丘谷商城采购商申请无法通过审核,请联系管理员【丘丘谷】';
                }

                $this->load->library('util/Util_Sms', array(
                    'template' => $tpl
                ), 'UtilSms');

                $this->UtilSms->word($item['phone']);

                $send['state'] = true;
                break;
            }

            $send['error'] = '审核失败';

        } while(false);
        $this->output->send($send);
    }

}

